﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Text;
using Model;
using System.Configuration;
using System.Data;

namespace TCDC_System.Attribute.xml
{
    /// <summary>
    /// Summary description for CampaignGroup
    /// </summary>
    public class CampaignGroup : IHttpHandler
    {
        private DBUtil DB;
        XmlTextWriter objX;

        public void ProcessRequest(HttpContext context)
        {

            this.DB = new DBUtil();
            DB.strConn = ConfigurationManager.AppSettings["ConnString"];

            string sql = "SELECT * FROM tcdc_mst_campaign_group " +
                         "ORDER BY campaign_group_id ";
            DataTable dt = DB.ExecuteReader(sql);

            context.Response.Clear();
            context.Response.ContentType = "text/xml";

            objX = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8);
            objX.WriteStartDocument();
            objX.WriteStartElement("groups");

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {

                    objX.WriteStartElement("group");
                    objX.WriteElementString("group_id", row["campaign_group_id"].ToString());
                    objX.WriteElementString("group_name_th", row["campaign_group_name_th"].ToString());
                    objX.WriteElementString("group_name_en", row["campaign_group_name_en"].ToString());
                    objX.WriteEndElement();
                }
            }
            else
            {
                objX.WriteStartElement("group");
                objX.WriteElementString("group_id", "");
                objX.WriteElementString("group_name_th", "");
                objX.WriteElementString("group_name_en", "");
                objX.WriteEndElement();
            }

            objX.WriteEndElement();
            objX.WriteEndDocument();
            objX.Flush();
            objX.Close();
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}